package cn.uppower.wordaliy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.model.UserInfo;
import cn.uppower.wordaliy.WordaliyApplication;
import cn.uppower.wordaliy.util.MD5Utils;
import cn.uppower.wordaliy.util.PhoneFormatCheckUtils;
import cn.uppower.wordaliy.widget.BaseActivity;

public class RegisterActivity extends BaseActivity{

    private static final int GET_MESSAGE_CODE = 1;
    private ImageView iv_goback;
    private EditText et_phone,et_yanzhengma,et_one_pwd,et_sure_pwd;
    private Button btn_get_yanzhengma,btn_register;
    private TextView tv_xieyi,tv_title;
    private int nowSecond = 60;
    private boolean isGetMessageCode=false;
    private String phone;


    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case GET_MESSAGE_CODE:
                    if (nowSecond!=0){
                        btn_get_yanzhengma.setEnabled(false);
                        btn_get_yanzhengma.setText(nowSecond+"s");
                        nowSecond--;
                        handler.sendEmptyMessageDelayed(GET_MESSAGE_CODE,1000);
                    }else {
                        btn_get_yanzhengma.setEnabled(true);
                        btn_get_yanzhengma.setText("再次获取");
                        nowSecond=60;
                    }
                    break;
            }


        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public Integer getLayoutResId() {
        return R.layout.activity_register;
    }

    @Override
    public void init() {
        et_phone = findViewById(R.id.et_phone);
        iv_goback = findViewById(R.id.iv_goback);
        et_yanzhengma = findViewById(R.id.et_yanzhengma);
        btn_get_yanzhengma = findViewById(R.id.btn_get_yanzhengma);
        et_one_pwd = findViewById(R.id.et_one_pwd);
        et_sure_pwd = findViewById(R.id.et_sure_pwd);
        tv_xieyi = findViewById(R.id.tv_xieyi);
        btn_register = findViewById(R.id.btn_register);
        tv_title = findViewById(R.id.tv_title);
        tv_title.setText("Wordaliy");

//        返回点击事件
        iv_goback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        点击获取验证码
        btn_get_yanzhengma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_yanzhengma.setError(null);
                if (!checkPhone()){
                    return;
                }
                handler.sendEmptyMessage(GET_MESSAGE_CODE);
//                设置获取过验证码
                isGetMessageCode=true;

            }
        });


        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                先取消所有错误信息
                setNoErrorMessage();

                if (!isGetMessageCode){
//                    说明没有点击获取验证码
                    et_yanzhengma.setError("请先获取验证码");
                    return;
                }

                if (!checkPhone()){
                    return;
                }

                String pwd1= et_one_pwd.getText().toString().trim();
                String pwd2=et_sure_pwd.getText().toString().trim();

                if (pwd1.isEmpty()){
                    et_one_pwd.setError("密码不能为空");
                    return;
                }
                if (pwd2.isEmpty()){
                    et_sure_pwd.setError("密码不能为空");
                    return;
                }
                et_sure_pwd.setError(null);
                if (!pwd1.equals(pwd2)){
//                    说明两次密码不一致
                    et_sure_pwd.setError("两次密码不一致");
                    return;
                }

//                拿到手机号和密码进行注册
                if (et_yanzhengma.getText().toString().trim().equals("123456")){
//                    Toast.makeText(RegisterActivity.this, "手机号是:"+phone+" 密码是:"+pwd1, Toast.LENGTH_SHORT).show();
                    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    存储到本地数据库
                    UserInfo userInfo=new UserInfo(phone,simpleDateFormat.format(new Date()),MD5Utils.MD5Encode(pwd1,null));
                    boolean res= userInfo.save();
                    if (res){
//                        注册成功直接进入主界面
                        WordaliyApplication.userInfo=userInfo;
                        WordaliyApplication.wordaliyEditor.putString("userPhone",userInfo.getUserPhone());
                        WordaliyApplication.wordaliyEditor.putString("registeTime",userInfo.getRegisteTime());
                        WordaliyApplication.wordaliyEditor.commit();
                        startActivity(new Intent(RegisterActivity.this,MainActivity.class));
                    }else {
                        Toast.makeText(RegisterActivity.this, "注册失败，请稍后再试", Toast.LENGTH_SHORT).show();
                    }

                }else {
                    Toast.makeText(RegisterActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                }

            }
        });


        tv_xieyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(RegisterActivity.this, "暂无协议", Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void setNoErrorMessage() {
        et_phone.setError(null);
        et_one_pwd.setError(null);
        et_sure_pwd.setError(null);
        et_yanzhengma.setError(null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(GET_MESSAGE_CODE);
    }

    private boolean checkPhone(){
        phone=et_phone.getText().toString().trim();
        if (phone.isEmpty()){
            et_phone.setError("请先输入手机号码");
            return false;
        }
        if (!PhoneFormatCheckUtils.isPhoneLegal(phone)){
            et_phone.setError("手机号码不合法");
            return false;
        }
        return true;
    }
}
